package com.hanyao.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

/**
 * @author 寒钥
 * @version 1.0
 */
//使其自动创建实例注入到ioc容器中管理，自然要用到注解Component
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    //这边必须实现insertFill和updateFill，即在进行插入时自动填充和在进行更新操作时自动填充
    @Override
    public void insertFill(MetaObject metaObject) {
        //setValue有两个参数：一个是目标字段，一个是将要插入目标字段对应的值
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createTime", LocalDateTime.now());
        Long currentId = BaseContext.getCurrentId();
        metaObject.setValue("createUser", currentId);
        metaObject.setValue("updateUser",currentId);

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Long currentId = BaseContext.getCurrentId();
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser",currentId);
    }
}
